﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Com.CMS263.Util
{
    public static class UpLoadFileUtil
    {
       
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public static string UpLoadFile(System.Web.HttpPostedFile httpPostedFile, string savePath)
        {
            if(!savePath.EndsWith("/"))
            {
                savePath += "/";
            }
            string fileName = httpPostedFile.FileName;
            string ext = fileName.Substring(fileName.LastIndexOf('.')+1);
            string saveName = DateTime.Now.ToString("yyyyMMddhhmmss")+((new Random()).Next(10000)+10000).ToString()+"."+ext;
            httpPostedFile.SaveAs(savePath+saveName);
            return saveName;

        }
        /// <summary>
        /// 上传多个文件
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <param name="savePath"></param>
        /// <param name="key">上传文件序列</param>
        /// <returns></returns>
        public static string UpLoadFile(System.Web.HttpPostedFile httpPostedFile, string savePath, string key)
        {
            if (!savePath.EndsWith("/"))
            {
                savePath += "/";
            }
            string fileName = httpPostedFile.FileName;
            string ext = fileName.Substring(fileName.LastIndexOf('.') + 1);
            //if (ext.ToLower() != "gif" && ext.ToLower() != "jpg" && ext.ToLower() != "bmp" && ext.ToLower() != "png")
            //{
            //    StringUtil.Alert("图片格式错误", "-1");
            //    return "";
            //}
            if (httpPostedFile.ContentLength > 5)
            {
                string saveName = key + DateTime.Now.ToString("yyyyMMddhhmmss") + ((new Random()).Next(10000) + 10000).ToString() + "." + ext;
                httpPostedFile.SaveAs(savePath + saveName);
                return saveName;
            }
            else
            {
                return "";
            }

        }
        /// <summary>
        /// 上传Excel文件
        /// </summary>
        /// <param name="httpPostedFile"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public static string UpLoadFileExcel(System.Web.HttpPostedFile httpPostedFile, string savePath)
        {
            if (!savePath.EndsWith("/"))
            {
                savePath += "/";
            }
            
            string fileName = httpPostedFile.FileName;
            string ext = fileName.Substring(fileName.LastIndexOf('.') + 1);
            if (ext.ToLower() != "xls" && ext.ToLower() != "xlsx")
            {
                StringUtil.Alert("请上传Excel文件", "-1");
                return "";
            }
            if (httpPostedFile.ContentLength > 5)
            {
            string saveName = DateTime.Now.ToString("yyyyMMddhhmmss") + ((new Random()).Next(10000) + 10000).ToString() + "." + ext;
            httpPostedFile.SaveAs(savePath + saveName);
            return saveName;
            }
            else
            {
                return "";
            }
           

        }

        public static string UpLoadFileName(System.Web.HttpPostedFile httpPostedFile, string savePath)
        {
            if (!savePath.EndsWith("/"))
            {
                savePath += "/";
            }
            string fileName = httpPostedFile.FileName;
            
            if (fileName.LastIndexOf("\\") > 0)
            {
                fileName = fileName.Substring((fileName.LastIndexOf("\\")+1));
            }
             string fname = fileName.Substring(0,fileName.LastIndexOf("."));
            string ext = fileName.Substring(fileName.LastIndexOf('.') + 1);           
            //if (ext.ToLower() != "gif" && ext.ToLower() != "jpg" && ext.ToLower() != "bmp" && ext.ToLower() != "png")
            //{
            //    StringUtil.Alert("图片格式错误", "-1");
            //    return "";
            //}
            if (httpPostedFile.ContentLength > 5)
            {
                string savename = GetNoExistsFileName(savePath, fname,ext);
                httpPostedFile.SaveAs(savePath + savename);
                return savename;
            }
            else
            {
                return "";
            }
        }
        public static int i = 1;
        public static string GetNoExistsFileName(string path,string fname,string ext)
        {
            FileInfo file = new FileInfo(path+fname+"."+ext);
            if (file.Exists)
            {
                fname+="("+i+")";
                i++;
                return  GetNoExistsFileName(path,fname,ext);
            }
            else
            {
                i = 1;
                return  fname+"."+ext;
                
            }
        }
        /// <summary>
        /// 生成带有缩略图带水印的图片的图片
        /// </summary>
        /// <param name="httpPostedFile">文件域</param>
        /// <param name="savePath">保存路径</param>
        /// <param name="key">编号</param>
        /// <param name="sizeList">缩略图尺寸列表</param>
        /// <returns></returns>
        public static string UpLoadFile(System.Web.HttpPostedFile httpPostedFile, string savePath, string key, string sizeList)
        {
            string fileName = UpLoadFile(httpPostedFile, savePath, key);

            ImageUtil iu = new ImageUtil();
            //循环生成缩略图
            if(!string.IsNullOrEmpty(sizeList))
            {
                string[] sizeArr = sizeList.Split('|');

                foreach (string str in sizeArr)
                {
                    string[] s = str.Split('*');
                    iu.MakeThumbnail(fileName, "/ArtUpFile/" + DateTime.Now.ToString("yyyy-MM-dd") + "/", "/ArtUpFile/" + s[0] + "X" + s[1] + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/", int.Parse(s[0]), int.Parse(s[1]), "cut");
                }

            }
            
            return fileName;
        }
    }
}
